package com.app.dao.mysql;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListToVarcharTypeHandler  implements TypeHandler<List<Double>> {

    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, List<Double> doubles, JdbcType jdbcType) throws SQLException {
        // 遍历List类型的入参，拼装为String类型，使用Statement对象插入数据库
        StringBuffer sb = new StringBuffer();
        for (int j = 0; j < doubles.size(); j++) {
            if (j == doubles.size() - 1) {
                sb.append(doubles.get(j));
            } else {
                sb.append(doubles.get(j)).append(",");
            }
        }
        preparedStatement.setString(i, sb.toString());

    }

    @Override
    public List<Double> getResult(ResultSet resultSet, String s) throws SQLException {
        return null;
    }

    @Override
    public List<Double> getResult(ResultSet resultSet, int i) throws SQLException {
        return null;
    }

    @Override
    public List<Double> getResult(CallableStatement callableStatement, int i) throws SQLException {
        return null;
    }
}
